package cn.wolfcode.web.interceptor;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.util.UserContext;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录的拦截器
 */
public class LoginInterceptr implements HandlerInterceptor{
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
       //从session中获取当前用户
        //Object employee = request.getSession().getAttribute("USER_IN_SESSION");
        Employee employee = UserContext.getCurrentUser();
        if (employee == null) {//如果没有登录 不放行
            response.sendRedirect("/login.html");//重定向到登录页面
            return false;
        }
        //如果是管理员就直接放行
        return true;
    }
}
